<?php
class PostTopicModel extends MY_Model{
    public $table = 't_post_topic';
    public function __construct(){
        parent::__construct();
    }

    /**
     * 通过postid取出所有的话题和一级分类
     * name: get_post_topic
     * @param $id
     * @return array
     * @throws Exception
     * @Author:taiyang
     */
    public function get_post_topic($id){
        if(empty($id)){return array();}
        $sql="select t.*,t2.title as parent_title from ".$this->table." pt  
                left join t_topic t on t.id=pt.topic_id 
                left join t_topic t2 on t2.id=t.parent_id 
                where 1 and pt.post_id='".$id."' and pt.status=1 ";
        $res=$this->query_sql_list($sql);
        if(empty($res)){return array();}
        $ids=array();
        $class=array();
        foreach ($res as $k=>$v){
            $ids[]=$v['id'];
            if($v['parent_id']){
                $class[$v['parent_id']]=$v['parent_title'];
            }
        }
        return array("ids"=>$ids,'class'=>implode('/',$class),'list'=>$res);
    }

    /**
     * 添加删除话题
     * name: del_post_topic
     * @param $post_id
     * @param $topic_id
     * @param int $type 1添加2删除
     * @return bool
     * @throws Exception
     * @Author:taiyang
     */
    public function del_add_post_topic($post_id,$topic_id,$type=1){
        if(empty($topic_id) || empty($post_id)){return false;}
        $res=$this->PostTopicModel->query_one(array('post_id'=>$post_id,'topic_id'=>$topic_id));
        if($type == 1){
            if(empty($res)){
                $this->PostTopicModel->insert_one(array('post_id'=>$post_id,'topic_id'=>$topic_id));
            }else{
                $this->PostTopicModel->update_data(array('status'=>1),array('post_id'=>$post_id,'topic_id'=>$topic_id));
            }
            $num="+1";
        }else{
            $this->PostTopicModel->update_data(array('status'=>-1),array('post_id'=>$post_id,'topic_id'=>$topic_id));
            $num="-1";
        }
        //修改数据
        $this->TopicModel->db->set('post_num',"post_num".$num."",false);
        $this->TopicModel->db->where('id', $topic_id);
        $this->TopicModel->db->update('t_topic');
        return true;
    }


    /**
     * 生成推荐话题 标签
     * name: get_topic
     * @param string $str
     * @param array $topic_arr
     * @param array $new_tag
     * @return string
     * @Author:taiyang
     */
    public function get_topic($str='',$topic_arr=array(),$new_tag=array()){
        $this->load->library('SplitWord');
        header('Content-Type:text/html;charset=utf-8');
        ini_set('memory_limit','30M');
        $res = $this->splitword->SplitRMM($str);
        $split_arr=explode(' ',$res);
        $new_topic=array_intersect($topic_arr,$split_arr);
        $new_tag=array_intersect($new_tag,$split_arr);
        $string='';
        $string.="推荐标签：";
        if(empty($new_tag)){
            $string.='无';
        }else{
            $string.=implode(',',array_slice($new_tag,0,17));
        }
        $string.="<br>推荐话题：";
        if(empty($new_topic)){
            $string.='无';
        }else{
            $string.=implode(',',$new_topic);
        }
        return $string;
    }
    /**
     * 生成推荐话题 标签  批量操作专用
     * name: get_topic
     * @param string $str
     * @param array $topic_arr
     * @param array $new_tag
     * @return string
     * @Author:taiyang
     */
    public function get_tag($str='',$topic_arr=array(),$new_tag=array(),$id=''){
        $this->load->library('SplitWord');
        header('Content-Type:text/html;charset=utf-8');
        ini_set('memory_limit','30M');
        $res = $this->splitword->SplitRMM($str);
        $split_arr=explode(' ',$res);
        $new_topic=array_intersect($topic_arr,$split_arr);
        $new_tag=array_intersect($new_tag,$split_arr);
        $i=0;
        echo "帖子id".$id."<br>";
        foreach ($new_tag as $k=>$v){
            $i++;
            if($i<12){
                $this->PostTagModel->del_add_post_tag($id,$k,1);
                echo 'tag:'.$k."<br>";
            }
        }
        $i=0;
        foreach ($new_topic as $key=>$vel){
            $i++;
            if($i<12) {
                $this->PostTopicModel->del_add_post_topic($id, $key, 1);
                echo 'topic:'.$key."<br>";
            }
        }
        echo "<br><br>";
        return true;
    }

}